package com.hnhcyy.b2b.receiver.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hnhcyy.k3cloud.client.HlyDdjylsClient;
import com.hnhcyy.k3cloud.pojo.HlyDdjyls;
import com.hnhcyy.k3cloud.pojo.Zhifjg;
import com.rabbitmq.client.Channel;

/**
 * Title: OrderFlowReceiver  
* Description:  中信通道的支付流水
* @author sheyongjun  
* @date 2019年3月2日
 */
//@Component
public class CiticPayFlowReceiver {

	private Logger logger = LoggerFactory.getLogger(getClass());
	//金蝶交易流水
	@Autowired
	private HlyDdjylsClient hlyDdjylsClient;

	/**
	 * 微信和支付宝支付统一
	 * @author sheyongjun
	 * @date 2019年3月2日
	 */
//	@RabbitListener(queues = "b2b.order.citicpay.flow")
	public void citicpay(Message message, Channel channel) throws Exception {
		String text = new String(message.getBody(), "utf-8");
		System.out.println("b2b.order.citicpay.flow:" + text);
		try {
			JSONObject jsonObject = JSONObject.parseObject(text);
			System.out.println(jsonObject.get("out_trade_no"));
			HlyDdjyls hlyDdjyls = new HlyDdjyls();
			
			Zhifjg zhifjgObj = new Zhifjg();
			// pay.weixin.jspay/ pay.weixin.native/ pay.alipay.native
			String tradeType = jsonObject.getString("trade_type"); 
			//JSFS31_SYS：微信 / JSFS32_SYS 支付宝
			String jylx = null;
			if("pay.weixin.native".equals(tradeType)) { // 微信扫码
				zhifjgObj.setFNumber("JSFS31_SYS");
				jylx = "PC-微信";
			}else if("pay.weixin.jspay".equals(tradeType)){ // 微信公众号
				zhifjgObj.setFNumber("JSFS31_SYS");
				jylx = "公众号-微信";
			}else if("pay.alipay.native".equals(tradeType)){ // 支付宝扫码
				zhifjgObj.setFNumber("JSFS32_SYS");
				jylx = "PC-支付宝";
			}
			
			String shanghname = "中信商户"; // 商户号
			String shanghddh = jsonObject.getString("out_trade_no");
			String shanghddrq = jsonObject.getString("time_end");
			String ddje = jsonObject.getIntValue("total_fee")/100.0 + "";// 订单的金额
			String zhifje = jsonObject.getIntValue("cash_fee")/100.0 + "" ;// 用户实际支付的金额
			String dingdzt = jsonObject.getString("result_code");
			String cpddh = jsonObject.getString("out_transaction_id");
			String cplydh = jsonObject.getString("transaction_id");
			String beizhu = jsonObject.getString("");
			String zhongdh = jsonObject.getString("mch_id");
			
			
			hlyDdjyls.setFBillNo("").setF_shanghname(shanghname).setF_zhifjg(zhifjgObj)
			.setF_shanghddh(shanghddh).setF_shanghddrq(shanghddrq).setF_ddje(ddje)
			.setF_zhifje(zhifje).setF_cpddh(cpddh).setF_jylx(jylx).setF_dingdzt(dingdzt).setF_cpckdh("")
			.setF_cpddh(cpddh).setF_cplydh(cplydh).setF_zhongdh(zhongdh).setF_beizhu(beizhu);
			boolean tag = hlyDdjylsClient.add(hlyDdjyls);
			if(tag) {
				channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
			}else {
				//ack返回false，并重新回到队列
				channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("======金蝶交易流水保存出错======");
			//丢弃这条消息（绑定了死信队列，该消息会保存到死信队列）
			channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
		}
	}
	
	
	/*public static void main(String[] args) {
		String text = "{\"transaction_id\":\"102545761422201903021201851975\",\"charset\":\"UTF-8\",\"nonce_str\":\"1551498582947\","
				+ "\"bank_type\":\"COMM_CREDIT\",\"openid\":\"oMJGHs4Gdmwjn8-joV--HFctYxHM\",\"sign\":\"UeJ8pYEdoWkSkMh+QX1yf4F1v7ZMFDZGQ9dWXvbNd9sL3H1SpQM5btzrGSXBInCb0jQycgWdyVBMgxfAm9lURMZx8MYczqAsxcIxUv2Knjwet8kivJJvQikGvaEcMj+k3t3ReYKsMclVkQN34bMXlZ3yF0/SyuBBEnhvBDUNC90Or4EjTL/dnkP2pOW8dssGoa/dlx/DGBgbLOavhb3IJjdN6YG2ix7HmAJbA9KyQlOVnpbmuu2J+ZcbqkaGn0GBEgnzLFs7QBN+36L+GzIChPrFFhyVFzK+BzDNyd/VP3YqvMDBDTrpZ949sse6iYZxXXE73MtLKeMGgLGpUpmHTA==\","
				+ "\"out_transaction_id\":\"4200000242201903025718554966\",\"fee_type\":\"CNY\",\"mch_id\":\"102545761422\",\"sub_appid\":\"wx4696e4b4b8ae570d\","
				+ "\"version\":\"2.0\",\"cash_fee\":\"194800\",\"pay_result\":\"0\",\"sub_openid\":\"obl16uHREvbEdISQ_kjgicG1Noog\","
				+ "\"out_trade_no\":\"19030211483500013259\",\"coupon_fee\":\"0\",\"total_fee\":\"194800\",\"trade_type\":\"pay.weixin.native\","
				+ "\"result_code\":\"0\",\"time_end\":\"20190302114942\",\"sign_type\":\"RSA_1_256\",\"status\":\"0\"}";
		JSONObject jsonObject = JSONObject.parseObject(text);
		HlyDdjyls hlyDdjyls = new HlyDdjyls();
		
		Zhifjg zhifjgObj = new Zhifjg();
		// pay.weixin.jspay/ pay.weixin.native/ pay.alipay.native
		String tradeType = (String) jsonObject.get("trade_type"); 
		//JSFS31_SYS：微信 / JSFS32_SYS 支付宝
		String jylx = "";
		if("pay.weixin.native".equals(tradeType)) { // 微信扫码
			zhifjgObj.setFNumber("JSFS31_SYS");
			jylx = "PC-微信";
		}else if("pay.weixin.jspay".equals(tradeType)){ // 微信公众号
			zhifjgObj.setFNumber("JSFS31_SYS");
			jylx = "公众号-微信";
		}else if("pay.alipay.native".equals(tradeType)){ // 支付宝扫码
			zhifjgObj.setFNumber("JSFS32_SYS");
			jylx = "PC-支付宝";
		}
		
		String shanghh = "中信商户"; // 商户号
		String shanghddh = (String) jsonObject.get("out_trade_no");
		String shanghddrq = (String) jsonObject.get("time_end");
		String ddje = Double.parseDouble((String) jsonObject.get("total_fee"))/100 + "";// 订单的金额
		String zhifje = Double.parseDouble((String) jsonObject.get("cash_fee"))/100 + "" ;// 用户实际支付的金额
		String dingdzt = (String) jsonObject.get("result_code");
		String cpddh = (String) jsonObject.get("out_transaction_id");
		String cplydh = (String) jsonObject.get("transaction_id");
		String beizhu = (String) jsonObject.get("");
		String zhongdh = (String) jsonObject.get("mch_id");
		
		
		hlyDdjyls.setFBillNo("").setF_shanghh(shanghh).setF_shanghname("").setF_zhifjg(zhifjgObj)
		.setF_shanghddh(shanghddh).setF_shanghddrq(shanghddrq).setF_ddje(ddje)
		.setF_zhifje(zhifje).setF_cpddh(cpddh).setF_jylx(jylx).setF_dingdzt(dingdzt).setF_cpckdh("")
		.setF_cpddh(cpddh).setF_cplydh(cplydh).setF_zhongdh(zhongdh).setF_beizhu(beizhu);
	}*/

}
